<?php
namespace app\bis\controller;

class Location extends Base
{
	public function index(){
		$locations = model('bis_location')->getLocationByBisId($this->account->bis_id);
		$this->assign('locations',$locations);
		return $this->fetch();
	}

	public function add(){
		if(request()->isPost()){

			$data = input('post.');
			//获取经纬度
	        $lnglat = \Map::getLngLat($data['address']);
	        // var_dump($lnglat);die;
	        if(empty($lnglat) || $lnglat['status'] != 0 || $lnglat['result']['precise'] != 1){
	            $this->error('无法获取准确地址，或地址匹配不准确，请核实地址输入是否有误');
	        }


	        //总店相关信息校验
	        $data['cat'] = '';

	        if(!empty($data['se_category_id'])){
	            $data['cat'] = implode(',',$data['se_category_id']);
	        }

	        //校验数据 validate

	        $bisId = session('BisAccount','','bis')->bis_id;
	        if(!$bisId){
				$this->redirect(url('login/index'));
			}

	        //分店相关信息入库
	        $locationData = [
	            'bis_id' => $bisId,
	            'name' => $data['name'],
	            'logo' => $data['logo'],
	            'tel' => $data['tel'],
	            'contact' => $data['contact'],
	            'category_id' => $data['category_id'],
	            'category_path' => $data['category_id'].','.$data['cat'],
	            'city_id' => $data['city_id'],
	            'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'],
	            'api_address' => $data['address'],
	            'content' => empty($data['content']) ? '' : $data['content'],
	            'is_main' => 0,//代表总店信息
	            'xpoint' => empty($lnglat['result']['location']['lng']) ? '' : $lnglat['result']['location']['lng'],
	            'ypoint' => empty($lnglat['result']['location']['lat']) ? '' : $lnglat['result']['location']['lat'],
	        ];

	        $locationId = model('BisLocation')->add($locationData);

	        if($locationId){
	        	return $this->success('门店申请成功');
	        }else{
	        	return $this->error('门店申请失败');
	        }

		}

		$citys = model('city')->getCitysByParentId();
        $categorys = model('category')->getCategorysByParentId();
        $this->assign([
            'citys' => $citys,
            'categorys' => $categorys,
        ]);
		return $this->fetch();
	}
}
